home *** CD-ROM | disk | FTP | other *** search
- class CSimulation
- {
- var m_arrClocks;
- var m_arrDeltaTimes;
- var m_arrTimeScalars;
- var m_arrIsPaused;
- var m_vecGravity;
- var m_bodies;
- var m_simulatedThings;
- var m_collisionResolver;
- var m_nUpdates = 0;
- var m_timeSecondsReal = 0;
- var m_timeSecondsSimulated = 0;
- var m_frameDeltaTimeReal = 0;
- var m_frameDeltaTimeSimulated = 0;
- var m_constantFrameDeltaTime = 0;
- var m_timeScalar = 1;
- var m_isPaused = false;
- var m_bCheckCollisions = true;
- var m_shouldSortThings = false;
- var m_maxFrameDeltaTime = 0.1;
- function CSimulation(collisionResolver)
- {
- this.m_timeSecondsReal = new Date().getTime() / 1000;
- this.m_timeScalar = 1;
- this.m_arrClocks = new Array();
- this.m_arrDeltaTimes = new Array();
- this.m_arrTimeScalars = new Array();
- this.m_arrIsPaused = new Array();
- this.m_vecGravity = new Vector2D(0,0);
- this.m_bodies = new Array();
- this.m_simulatedThings = new Array();
- if(collisionResolver)
- {
- this.m_collisionResolver = collisionResolver;
- }
- else
- {
- this.m_collisionResolver = new CCollisionResolverBucketSort(8,6,800,600,0,0);
- }
- }
- function DestroyAllBodies()
- {
- while(this.m_bodies.length > 0)
- {
- this.m_bodies[0].Destroy();
- }
- }
- function CreateNamedClock(name)
- {
- this.m_arrClocks[name] = 0;
- this.m_arrDeltaTimes[name] = 0;
- this.m_arrTimeScalars[name] = 1;
- this.m_arrIsPaused[name] = false;
- }
- function HasNamedClock(name)
- {
- return this.m_arrClocks[name] != undefined;
- }
- function SetPausedNamedClock(name, isPaused)
- {
- if(name == "")
- {
- this.m_isPaused = isPaused;
- }
- else
- {
- this.m_arrIsPaused[name] = isPaused;
- }
- }
- function SetNamedTimeScalar(name, timeScalar)
- {
- if(name == "")
- {
- this.m_timeScalar = timeScalar;
- }
- else
- {
- this.m_arrTimeScalars[name] = timeScalar;
- }
- }
- function GetNamedClock(name)
- {
- if(name == "")
- {
- return this.m_timeSecondsSimulated;
- }
- return this.m_arrClocks[name];
- }
- function GetNamedDeltaTime(name)
- {
- if(name == "")
- {
- return this.m_frameDeltaTimeSimulated;
- }
- return this.m_arrDeltaTimes[name];
- }
- function GetNamedTimeScalar(name)
- {
- if(name == "")
- {
- return this.m_timeScalar;
- }
- return this.m_arrTimeScalars[name];
- }
- function GetNamedIsPaused(name)
- {
- if(name == "")
- {
- return this.m_isPaused;
- }
- return this.m_arrIsPaused[name];
- }
- function UpdateNamedClock(name)
- {
- FreshDebug.Assert(this.HasNamedClock(name),"HasNamedClock( name )");
- if(!this.m_arrIsPaused[name])
- {
- if(this.m_constantFrameDeltaTime)
- {
- this.m_arrDeltaTimes[name] = this.m_constantFrameDeltaTime * this.m_arrTimeScalars[name];
- }
- else
- {
- this.m_arrDeltaTimes[name] = this.m_frameDeltaTimeReal * this.m_arrTimeScalars[name];
- }
- this.m_arrDeltaTimes[name] = Math.min(this.m_arrDeltaTimes[name],this.m_maxFrameDeltaTime);
- this.m_arrClocks[name] += this.m_arrDeltaTimes[name];
- }
- else
- {
- this.m_arrDeltaTimes[name] = 0;
- }
- }
- function UpdateAllClocks()
- {
- var _loc3_ = this.m_timeSecondsReal;
- this.m_timeSecondsReal = new Date().getTime() / 1000;
- this.m_frameDeltaTimeReal = this.m_timeSecondsReal - _loc3_;
- if(!this.m_isPaused)
- {
- if(this.m_constantFrameDeltaTime)
- {
- this.m_frameDeltaTimeSimulated = this.m_constantFrameDeltaTime * this.m_timeScalar;
- }
- else
- {
- this.m_frameDeltaTimeSimulated = this.m_frameDeltaTimeReal * this.m_timeScalar;
- }
- this.m_frameDeltaTimeSimulated = Math.min(this.m_frameDeltaTimeSimulated,this.m_maxFrameDeltaTime);
- this.m_timeSecondsSimulated += this.m_frameDeltaTimeSimulated;
- }
- else
- {
- this.m_frameDeltaTimeSimulated = 0;
- }
- for(var _loc2_ in this.m_arrClocks)
- {
- this.UpdateNamedClock(_loc2_);
- }
- }
- function Update()
- {
- this.m_nUpdates = this.m_nUpdates + 1;
- this.UpdateAllClocks();
- if(this.m_shouldSortThings)
- {
- this.m_simulatedThings.sortOn("_updatePriority");
- }
- var _loc4_ = 0;
- while(_loc4_ < this.m_simulatedThings.length)
- {
- this.m_simulatedThings[_loc4_].PreUpdate();
- _loc4_ = _loc4_ + 1;
- }
- _loc4_ = 0;
- while(_loc4_ < this.m_simulatedThings.length)
- {
- this.m_simulatedThings[_loc4_].Update();
- _loc4_ = _loc4_ + 1;
- }
- var _loc2_ = true;
- var _loc5_ = 0;
- while(_loc2_ && _loc5_ < 4)
- {
- _loc2_ = false;
- _loc4_ = 0;
- while(_loc4_ < this.m_simulatedThings.length)
- {
- var _loc3_ = this.m_simulatedThings[_loc4_].UpdateConstraints();
- _loc2_ = _loc2_ || _loc3_;
- _loc4_ = _loc4_ + 1;
- }
- _loc5_ = _loc5_ + 1;
- }
- _loc4_ = 0;
- while(_loc4_ < this.m_simulatedThings.length)
- {
- this.m_simulatedThings[_loc4_].PostUpdate();
- _loc4_ = _loc4_ + 1;
- }
- if(this.m_collisionResolver && this.m_bCheckCollisions)
- {
- this.m_collisionResolver.ResolveCollisions(this.m_bodies);
- }
- }
- function FindBody(body)
- {
- var _loc2_ = 0;
- while(_loc2_ < this.m_bodies.length)
- {
- if(this.m_bodies[_loc2_] == body)
- {
- return _loc2_;
- }
- _loc2_ = _loc2_ + 1;
- }
- return -1;
- }
- function RegisterBody(body)
- {
- var _loc2_ = this.FindBody(body);
- if(_loc2_ < 0)
- {
- this.m_bodies.push(body);
- this.RegisterSimulated(body);
- }
- }
- function UnregisterBody(body)
- {
- var _loc2_ = this.FindBody(body);
- if(_loc2_ >= 0)
- {
- this.m_bodies.splice(_loc2_,1);
- }
- this.UnregisterSimulated(body);
- }
- function FindSimulated(simulated)
- {
- var _loc2_ = 0;
- while(_loc2_ < this.m_simulatedThings.length)
- {
- if(this.m_simulatedThings[_loc2_] == simulated)
- {
- return _loc2_;
- }
- _loc2_ = _loc2_ + 1;
- }
- return -1;
- }
- function RegisterSimulated(simulated)
- {
- var _loc2_ = this.FindSimulated(simulated);
- if(_loc2_ < 0)
- {
- this.m_simulatedThings.push(simulated);
- }
- }
- function UnregisterSimulated(simulated)
- {
- var _loc2_ = this.FindSimulated(simulated);
- if(_loc2_ >= 0)
- {
- this.m_simulatedThings.splice(_loc2_,1);
- }
- }
- }
-